
package com.jf.cloud.product.service.impl;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.StockChangeReasonDTO;
import com.jf.cloud.product.mapper.StockChangeReasonLangMapper;
import com.jf.cloud.product.mapper.StockChangeReasonMapper;
import com.jf.cloud.product.model.StockChangeReason;
import com.jf.cloud.product.model.StockChangeReasonLang;
import com.jf.cloud.product.service.StockChangeReasonLangService;
import com.jf.cloud.product.service.StockChangeReasonService;
import com.jf.cloud.product.vo.StockChangeReasonVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 出入库原因
 *
 * @author zz
 * @date 2022-06-22 16:57:27
 */
@Service
public class StockChangeReasonServiceImpl implements StockChangeReasonService {

    @Autowired
    private StockChangeReasonMapper stockChangeReasonMapper;

    @Autowired
    private StockChangeReasonLangMapper stockChangeReasonLangMapper;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private StockChangeReasonLangService stockChangeReasonLangService;

    @Override
    public PageVO<StockChangeReasonVO> page(PageDTO pageDTO, StockChangeReasonDTO stockChangeReasonDTO) {
        return PageUtil.doPage(pageDTO, () -> stockChangeReasonMapper.pageByParams(stockChangeReasonDTO));
    }

    @Override
    public StockChangeReasonVO getByStockChangeReasonId(Long stockChangeReasonId) {
        StockChangeReasonVO stockChangeReasonVO = mapperFacade.map(stockChangeReasonMapper.getByStockChangeReasonId(stockChangeReasonId), StockChangeReasonVO.class);
        StockChangeReasonLang stockChangeReasonLang =stockChangeReasonLangMapper.getByStockChangeReasonId(stockChangeReasonId);
        stockChangeReasonVO.setReason(stockChangeReasonLang.getReason());
        stockChangeReasonVO.setRemark(stockChangeReasonLang.getRemark());
        return stockChangeReasonVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        // 校验信息
        this.checkInfo(stockChangeReasonDTO, true);
        // 设置默认值
        stockChangeReasonDTO.setStatus(StatusEnum.ENABLE.value());
        // 保存出入库原因
        StockChangeReason  stockChangeReason = mapperFacade.map(stockChangeReasonDTO, StockChangeReason.class);
        stockChangeReason.setStatus(StatusEnum.ENABLE.value());
        stockChangeReasonMapper.save(stockChangeReason);
        StockChangeReasonLang stockChangeReasonLang = mapperFacade.map(stockChangeReasonDTO, StockChangeReasonLang.class);
        stockChangeReasonLang.setStockChangeReasonId(stockChangeReason.getStockChangeReasonId());
        stockChangeReasonLang.setLang(0);
        stockChangeReasonLangMapper.save(stockChangeReasonLang);
    }

    @Override
    public void update(StockChangeReason stockChangeReason) {
        StockChangeReasonDTO stockChangeReasonDTO = mapperFacade.map(stockChangeReason, StockChangeReasonDTO.class);
        // 校验信息
        this.checkInfo(stockChangeReasonDTO, false);
        stockChangeReasonMapper.update(stockChangeReason);
    }

    @Override
    public void deleteById(Long stockChangeReasonId) {
        stockChangeReasonMapper.deleteById(stockChangeReasonId);
        stockChangeReasonLangMapper.deleteById(stockChangeReasonId);
    }

    @Override
    public void changeStatus(Long shopId, Long stockChangeReasonId, Integer status) {
        StockChangeReason stockChangeReason = stockChangeReasonMapper.getByStockChangeReasonId(stockChangeReasonId);
        if(Objects.equals(stockChangeReason.getShopId(), Constant.PLATFORM_SHOP_ID) && !Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)){
            // 商家端改变系统内置的出入库原因状态
            if (Objects.equals(status, StatusEnum.DELETE.value())) {
                //系统内置出入库原因不能删除
                throw new LuckException("系统内置出入库原因不能删");
            }
        }else {
            stockChangeReason.setStatus(status);
            stockChangeReasonMapper.update(stockChangeReason);
        }
    }

    @Override
    public List<StockChangeReasonVO> list(StockChangeReasonDTO stockChangeReasonDTO) {
        return stockChangeReasonMapper.getByShopId(stockChangeReasonDTO);
    }

    /**
     * 校验信息
     * @param stockChangeReasonDTO 出入库原因
     * @param saveOrUpdate true: 保存时校验  false: 更新时校验
     */
    private void checkInfo(StockChangeReasonDTO stockChangeReasonDTO, Boolean saveOrUpdate) {
        if (!saveOrUpdate && Objects.isNull(stockChangeReasonDTO.getStockChangeReasonId())) {
            throw new LuckException("id不能为空");
        }
        int count = stockChangeReasonLangService.countReason(stockChangeReasonDTO);
        if (count > 0) {
            throw new LuckException("出入库原因不能重复！");
        }

    }
}
